//package com.gxnzd.communityelderly.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.gxnzd.communityelderly.commom.BaseContext;
//import com.gxnzd.communityelderly.commom.JwtUtils;
//import com.gxnzd.communityelderly.commom.R;
//import com.gxnzd.communityelderly.entity.Employee;
//import jakarta.annotation.Resource;
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.http.HttpMethod;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.bind.annotation.CrossOrigin;
//
//import java.io.IOException;
//
///**
// * ClassName: LoginFilter
// * Package: com.gxnzd.communityelderly.filter
// * Description:
// *
// * @Author 胡海峰
// * @Create 2024/10/5 17:22
// * @Version 1.0
// */
////@WebFilter(urlPatterns = "/*")
//    @Component
//public class LoginFilter implements Filter {
//    @Resource
//    private JwtUtils jwtUtils;
//    //匹配路径的工具
//    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//
//
//        //1、获取本次请求的URI
//        String requestURI = request.getRequestURI();
//        //定义不需要处理的请求路径
//        String[] urls = new String[]{
//                "/employee/login",
//                "/employee/logout",
//        };
//        //2、判断本次请求是否需要处理
//        boolean check = check(urls, requestURI);
//        //3、如果不需要处理，则直接放行
//        if(check){
//            filterChain.doFilter(request,response);
//            return;
//        }
////        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
////            filterChain.doFilter(request,response);
////            return;
////        }
//        //4-1、判断登录状态，如果已登录，则直接放行
//        //获取token
//        String authorityToken = request.getHeader("Authorization");
//        System.out.println(authorityToken);
//        boolean expired = jwtUtils.isTokenExpired(authorityToken);
//        System.out.println(expired);
////        if(expired){
//////            Long empId = (Long) request.getSession().getAttribute("empId");
////            jwtUtils.getUserInfoFromToken(authorityToken, Employee.class);
////            System.out.println(expired);
//////            BaseContext.setThreadLocal();
////            filterChain.doFilter(request,response);
////            return;
////        }
//
//
//        //5、如果未登录则返回未登录结果，通过输出流方式向客户端页面响应数据
//        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//        return;
//    }
//
//
//    public boolean check(String[] urls, String requestURI) {
//        for (String url : urls) {
//            boolean match = PATH_MATCHER.match(url, requestURI);
//            if (match) {
//                return true;
//            }
//        }
//        return false;
//    }
//}
